package fz.vrd.library.permissions;


import android.app.Activity;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import fz.vrd.library.log.LogFactory;

/**
 * @创建：VinRichard 2021/10/25 0025
 * @说明 ：权限管理
 * @备注 ：
 */

public class PermissionsManage {

    public final static  int REQUEST_CODE = 0x58;

    String[] permissions;

    RequestPermissionsCallback callBack;

    Context context;


    public PermissionsManage setActivity(Context context) {
        this.context = context;
        return this;
    }

    public PermissionsManage setPermissions(String... permissions) {
        this.permissions = permissions;
        return this;
    }

    public PermissionsManage setCallBack(RequestPermissionsCallback callBack) {
        this.callBack = callBack;
        return this;
    }

    public boolean isFirstRequestPermission(String permission){
        return shouldShowRequestPermissionRationale(permission);
    }

    public boolean shouldShowRequestPermissionRationale(String permission){
        Activity activity = getActivity();
        if(activity == null){
            return false;
        }
        return getActivity().shouldShowRequestPermissionRationale(permission);
    }

     Activity getActivity(){
        if(context != null){
            if(context instanceof  Activity){
                return (Activity) context;
            }else if(context instanceof ContextWrapper){
                return (Activity) ((ContextWrapper)context).getBaseContext();
            }
        }
        return null;
    }

    public void requestPermissions() {
        if (permissions != null && permissions.length > 0 && getActivity() != null) {
            boolean isTrue = true;
            for (String permission : permissions){
                if (!hasPermission(permission)) {
                    isTrue = false;
                    break;
                }
            }
            if(isTrue){
                if(callBack != null ){
                    callBack.success(permissions);
                }
            }else{
                getActivity().requestPermissions(permissions, REQUEST_CODE);
            }
        }
    }


    public boolean hasPermission( String permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true;

        return ContextCompat.checkSelfPermission(getActivity(), permissions) == PackageManager.PERMISSION_GRANTED;
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length > 0) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if(callBack != null){
                        callBack.success(permissions);
                    }
                } else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    if(callBack != null){
                        callBack.fail(false,permissions);
                    }
                }
            } else {
                if(callBack != null){
                    callBack.fail(true,permissions);
                }
            }
        }
    }




}
